import psutil


def get_process(pid=None, port=None):
    if port:
        for i in psutil.net_connections():
            if i.laddr.port == port:
                return psutil.Process(i.pid)
    elif pid:
        p = psutil.Process(pid)
        return p
    else:
        return None


if __name__ == '__main__':

    port = 8080  # 替换为你想要查询的端口号
    process = get_process(port=8080)
    print(process)
    if process:
        print(process.cmdline())
        print(process.exe())
        print(process.ppid())

    pp = process.parent()
    print(pp.pid, pp.name())
    if process:
        print(f"进程ID: {process.pid}, 进程名: {process.name()}")
    else:
        print("没有找到监听该端口的进程。")
